package com.exam.sacrifice.ui;

import android.os.Handler;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.exam.commonbiz.base.BaseFragment;
import com.exam.commonbiz.util.CollectionUtil;
import com.exam.commonbiz.util.OnFastClickListener;
import com.exam.sacrifice.R;
import com.exam.sacrifice.databinding.FragmentBaijiaxingBinding;
import com.exam.sacrifice.ui.adapter.BaiJiaXingListAdapter;
import com.exam.sacrifice.ui.baijiaxing.BaiJiaXingDetailActivity;
import com.exam.sacrifice.ui.bean.BaiJiaXingInfo;
import com.exam.sacrifice.ui.bean.BaiJiaXingListRes;
import com.exam.sacrifice.viewmodel.BaiJiaXingViewModel;
import com.exam.sacrifice.viewmodel.IBaiJiaXingCallBack;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class BaiJiaXingFragment extends BaseFragment<BaiJiaXingViewModel, FragmentBaijiaxingBinding> implements IBaiJiaXingCallBack, OnLoadMoreListener {

    private int pageIndex = 0;
    private String keyword;
    private BaiJiaXingListAdapter baiJiaXingListAdapter;

    public static BaiJiaXingFragment newInstance() {
        BaiJiaXingFragment fragment = new BaiJiaXingFragment();
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_baijiaxing;
    }

    @Override
    public void initData() {

        mViewModel.setBaiJiaXingCallBack(this);
        binding.smartRefreshLayout.setEnableLoadMore(false);
        binding.smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshData();
            }
        });
        binding.tvSearch.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                keyword = binding.edtContent.getText().toString();
                refreshData();
            }
        });

        baiJiaXingListAdapter = new BaiJiaXingListAdapter();
        LinearLayoutManager gridLayoutManager = new LinearLayoutManager(context);
        binding.recyclerView.setLayoutManager(gridLayoutManager);
        binding.recyclerView.setAdapter(baiJiaXingListAdapter);
        baiJiaXingListAdapter.setOnItemClickListener(new BaiJiaXingListAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(BaiJiaXingInfo item) {
                actionStart(BaiJiaXingDetailActivity.class);
            }
        });
        refreshData();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        loadMoreData();
    }

    private void refreshData() {
        pageIndex = 0;
        mViewModel.surnamePage(pageIndex, keyword);
    }

    private void loadMoreData() {
        ++pageIndex;
        mViewModel.surnamePage(pageIndex, keyword);
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void onBaiJiaXingList(BaiJiaXingListRes baiJiaXingListRes) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.smartRefreshLayout.finishRefresh();
            }
        }, 1500);
        binding.smartRefreshLayout.finishLoadMore();
        if (baiJiaXingListRes == null || baiJiaXingListRes.surnamePage == null) {
            return;
        }
        if (pageIndex == 0) {
            baiJiaXingListAdapter.setNewData(baiJiaXingListRes.surnamePage.records);
        } else {
            baiJiaXingListAdapter.addData(baiJiaXingListRes.surnamePage.records);
        }
        if (CollectionUtil.isEmpty(baiJiaXingListRes.surnamePage.records)) {
            binding.smartRefreshLayout.setEnableLoadMore(false);
            binding.smartRefreshLayout.finishLoadMoreWithNoMoreData();
        }
    }
}
